package com.juneng.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.juneng.common.R;
import com.juneng.config.RedisUtil;
import com.juneng.utils.Func;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import io.netty.util.internal.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import com.juneng.entity.UserAccount;
import com.juneng.dto.UserAccountDto;
import com.juneng.dto.UserAccountQueryDto;
import com.juneng.vo.UserAccountVo;
import com.juneng.service.IUserAccountService;

/**
 * 用户账号 控制器
 *
 * @author zds
 * @since 2021-07-08
 */
@RestController
@AllArgsConstructor
@RequestMapping("userAccount")
@Api(tags = "用户账号相关接口")
public class UserAccountController {

	private IUserAccountService userAccountService;

	private RedisUtil redisUtil;

	private ObjectMapper objectMapper;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperation(value = "详情", notes = "传入id")
	public R<UserAccount> detail(Long id) {
		UserAccount detail = userAccountService.getById(id);
		return R.success(detail);
	}

	/**
	 * 分页 用户账号
	 */
	@GetMapping("/page")
	@ApiOperation(value = "分页", notes = "传入userAccount")
	public R<IPage<UserAccountVo>> page(UserAccountQueryDto dto) {
		IPage<UserAccountVo> pages = userAccountService.page(dto);
		return R.success(pages);
	}
	/**
	 * 列表 用户账号
	 */
	@GetMapping("/list")
	@ApiOperation(value = "不分页", notes = "传入userAccount")
	public R<List<UserAccount>> list(UserAccountQueryDto dto) {
		List<UserAccount> list = userAccountService.list(dto);
		return R.success(list);
	}

	/**
	 * 新增 用户账号
	 */
	@PostMapping("/save")
	@ApiOperation(value = "新增", notes = "传入userAccount")
	public R save(@RequestBody UserAccountDto dto) {
		return R.success(userAccountService.save(dto));
	}

	/**
	 * 修改 用户账号
	 */
	@PutMapping("/update")
	@ApiOperation(value = "修改", notes = "传入userAccount")
	public R update(@RequestBody UserAccountDto dto) {
		return R.success(userAccountService.updateById(dto));
	}

	/**
	 * 删除 用户账号
	 */
	@DeleteMapping("/remove")
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.success(userAccountService.deleteLogic(Func.toIntList(ids)));
	}

	@GetMapping("/userInfo")
	@ApiOperation(value = "获取用户信息", notes = "传入token")
	public R<?> userInfo(@RequestHeader(value = "X-Token") String token) throws JsonProcessingException {
		if (StringUtil.isNullOrEmpty(token)) {
			//游客登入
			return R.failed("token不能为空");
		}
		UserAccount userAccount =(UserAccount) redisUtil.get(token);
         if (userAccount == null){
         	return R.failed("请重新登入");
		 }
		 Map<String, Object> map = new HashMap<>();
         map.put("roles","admin");
         map.put("nickname",userAccount.getAccountName());
         map.put("avatar",userAccount.getAvatar());
		return R.success(map);
	}
}
